package com.tepth.vehiclemaintenance.mvp.login;

import android.content.Intent;

import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.base.BaseMvpActivity;
import com.tepth.vehiclemaintenance.bean.User;
import com.tepth.vehiclemaintenance.config.SpKey;
import com.tepth.vehiclemaintenance.mvp.main.MainActivity;
import com.tepth.vehiclemaintenance.tools.ActivityTaskManager;
import com.tepth.vehiclemaintenance.tools.SPUtils;
import com.tepth.vehiclemaintenance.tools.ToastUtils;
import com.tepth.vehiclemaintenance.ui.view.ClearEditText;
import com.tepth.vehiclemaintenance.ui.view.dialog.DialogDelegate;
import com.tepth.vehiclemaintenance.ui.view.dialog.SweetAlertDialogDelegate;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyu on 2017/5/3.
 */

public class LoginActivity extends BaseMvpActivity implements LoginContract.View {
    @BindView(R.id.login_edtId)
    ClearEditText mEditTextAccount;
    @BindView(R.id.login_edtPwd)
    ClearEditText mEditTextPwd;

    LoginContract.Presenter mPresenter;
    DialogDelegate mDialogDelegate;

    @Override
    public void onNetworkError(String msg) {
        mDialogDelegate.stopProgressWithFailed(getString(R.string.login_failed), msg);
    }

    @Override
    public void onError(String msg) {
        mDialogDelegate.stopProgressWithFailed(getString(R.string.login_failed), msg);
    }

    @Override
    public void onTokenInvalid(String msg) {
        mDialogDelegate.stopProgressWithFailed(getString(R.string.login_failed), msg);
    }

    @Override
    public String getAccount() {
        return mEditTextAccount.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return mEditTextPwd.getText().toString().trim();
    }

    @Override
    public void showUser(User user) {
        //TODO 保存用户数据
        if (user != null) {
            SPUtils.putObject(this, SpKey.KEY_USER_INFO, user);
            SPUtils.putString(this, SpKey.KEY_USER_ACCOUNT, mEditTextAccount.getText().toString().trim());
            SPUtils.putString(this, SpKey.KEY_USER_PASSWORD, mEditTextPwd.getText().toString().trim());
            SPUtils.putString(this, SpKey.KEY_AUTH_KEY, user.getAuthKey());
        }
        mDialogDelegate.clearDialog();
        startActivity(new Intent(this, MainActivity.class));
        ActivityTaskManager.getActivityTaskManager().addActivity(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        mDialogDelegate = new SweetAlertDialogDelegate(this);
        //获取保存的账号密码
        String account = SPUtils.getString(this, SpKey.KEY_USER_ACCOUNT, "");
        String psd = SPUtils.getString(this, SpKey.KEY_USER_PASSWORD, "");
        if (!account.isEmpty() && !psd.isEmpty()) {
            mEditTextAccount.setText(account);
            mEditTextPwd.setText(psd);
            Intent intent = getIntent();
            if (!intent.hasExtra("reload")) {
                onLogin();
            }
        }
    }

    @Override
    public void setPresenter() {
        mPresenter = new LoginPresenter();
        LoginContract.Model model = new LoginModel();
        mPresenter.setVM(this, model);
    }

    @Override
    protected void unbindPresenter() {
        mPresenter.onDestroy();
    }

    @OnClick(R.id.login_btnLogin)
    public void onLogin() {
        if (mEditTextAccount.getText().toString().trim().isEmpty() ||
                mEditTextPwd.getText().toString().trim().isEmpty()) {
            ToastUtils.showToast(this, "请输入用户名密码");
            return;
        }
        mDialogDelegate.showProgressDialog(false, getString(R.string.login_ing));
        mPresenter.login();
    }
}
